public class Test3 {
    /*
    题目：
        写两个线程，两个线程轮流打印数字，从0一直打印到200
     */
    private static volatile int i = 0;

    public static void main(String[] args) {
        Object locker = new Object();
        Thread thread1 = new Thread() {
            @Override
            public void run() {
                while (true) {
                    synchronized (locker) {
                        locker.notify();
                        if (i == 200) {
                            break;
                        }
                        System.out.println("thread1 i = " + i++);
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        };

        Thread thread2 = new Thread() {
            @Override
            public void run() {
                while (true) {
                    synchronized (locker) {
                        locker.notify();
                        if (i == 200) {
                            break;
                        }
                        System.out.println("thread2 i = " + i++);
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        };

        thread1.start();
        thread2.start();
    }
}
